/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.RoleDTO;

/**
 *
 * @author User
 */
public  class RoleServiceImpl implements IEntity
{
    Statement stmt=null;
    ResultSet rst=null;
    private RoleDTO role;
    private List<RoleDTO> roleList;
    public RoleDTO GetById(int id) {
        try {
        	
            role = new RoleDTO();
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from `role` where idRole =" + id);
            role.setIdRole(id);
            role.setRoleDesc(rst.getString("roleDesc"));
            role.setroleTitle(rst.getString("roleTitle"));
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(RoleServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       return role;
    }

    public List<RoleDTO> GetAll() {

     try {
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from `role` ");
            while(rst.next()){
            	role.setIdRole(rst.getInt("idRole"));
            	role.setRoleDesc(rst.getString("roleDesc"));
            	role.setroleTitle(rst.getString("roleTitle"));
            	roleList.add(role);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(RoleServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
       return roleList;
    }

    public void Add(Object obj){
        
    	try {
        	
        	RoleDTO objCast=(RoleDTO)obj;
        	stmt = Configuration.GetStatement();

			stmt.execute("INSERT INTO dbsurvey.`role` (`idRole`, `roleTitle`, `roleDesc`) VALUES ("+objCast.getIdRole()+", '"+objCast.getRoleTitle()+"', '"+objCast.getRoleDesc()+"')");

			Configuration.CloseConnection();
         
 		} catch (SQLException e) {
 			
			// TODO Auto-generated catch block
			e.printStackTrace();
 		}
    }
    
    public void Update(Object obj) {
    	try {
    		
    		RoleDTO objCast=(RoleDTO)obj;
    		stmt = Configuration.GetStatement();
    		stmt.execute("update dbsurvey.`role` set  roleDesc="+objCast.getRoleDesc()+"and roleTitle="+objCast.getRoleTitle()+" where idRole="+objCast.getIdRole());
    		Configuration.CloseConnection();
      	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void Remove(int id) {
        try {
        	
        	stmt = Configuration.GetStatement();
        	stmt.execute("delete from `role`where idRole ="+id);
        	Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
